package main

import (
	"awesomeProject/gin_wzk/data_gorm"
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"net/http"
	"os/exec"
	"strconv"
)
func Login(ctx *gin.Context){
	ctx.HTML(200,"login/login.html",nil)
	//ctx.String(200,"login.html",nil)
}

func Ti(ctx *gin.Context)  {
	seesion := sessions.Default(ctx)
	username := ctx.PostForm("username")
	password := ctx.PostForm("password")
	_,err := exec.Command("/bin/sh", "-c", "python3.9 /home/wdr/kcbl.py "+username+" "+password).Output()
	if err != nil{
		ctx.JSON(200,gin.H{"code":500,"msg":"帐号或密码错误"})
	}else {
		seesion.Set("xh",username)
		seesion.Set("password",password)
		seesion.Save()
		ctx.JSON(200,gin.H{"code":200})
	}

}


func Middleware(ctx *gin.Context){
	seesion :=sessions.Default(ctx)

	var yyml  data_gorm.Yml
	xh :=seesion.Get("xh")
	//defer seesion.Delete("xh")
	data_gorm.Db.First(&yyml,"username = ?",xh)
	if xh != yyml.Username{
		ctx.Redirect(http.StatusFound,"/")
	}else {
		//seesion.Clear()
	}

}

func Test(ctx *gin.Context)  {
	seesion :=sessions.Default(ctx)
	xh :=seesion.Get("xh")
	var yyml  data_gorm.Yml
	var yyml2 []data_gorm.Yml2

	data_gorm.Db.First(&yyml,"username=?",xh)

	data_gorm.Db.Raw("select * from yml2 where username="+xh.(string)).Find(&yyml2)

	ctx.HTML(200,"list/member-list.html",gin.H{"list":yyml,"test":yyml2})

}
func Start(ctx *gin.Context){
	seesion :=sessions.Default(ctx)
	xh :=seesion.Get("xh")
	password :=seesion.Get("password")
	is_val := ctx.PostForm("is_val_wdr")
	id := ctx.PostForm("id")
	ids := ctx.PostForm("ids")
	Order := ctx.PostForm("Order1")
	var yym2 data_gorm.Yml2

	data_gorm.Db.First(&yym2,"username=? and id=?",id,ids)
	fmt.Println(is_val,id,ids,Order,xh,password)
	num, _ := strconv.Atoi(is_val)
	if num==0{
		data_gorm.Db.Model(&yym2).Update("js",1)
		exec.Command("/bin/sh", "-c", "nohup python3.9 -u /sk/wzk_sc.py "+xh.(string)+" "+password.(string)+" "+Order+" > /sk/file.log 2>&1 &").Output()
		ctx.SecureJSON(200,gin.H{"msg":"启动成功"})
	}else{
		data_gorm.Db.Model(&yym2).Update("js",0)
		shell := "`ps -aux | grep "+xh.(string)+" | grep "+Order+" | awk 'BEGIN{FS=\" \"};{print $2}'`"
		fmt.Println(shell)
		bod,err :=exec.Command("/bin/bash", "-c", "kill "+shell ).Output()
		//bod,err :=exec.Command("/bin/sh", "-c", "kill 63897").Output()
		//bod,err :=exec.Command("/bin/sh", "-c", "ls /sk").Output()
		fmt.Println(string(bod),err)

		ctx.SecureJSON(200,gin.H{"msg":"暂停刷课"})
	}



}

func Exit(ctx *gin.Context){
	seesion :=sessions.Default(ctx)
	seesion.Delete("xh")
	seesion.Clear()
	defer seesion.Save()

}
func main() {
	router := gin.Default()

	store := cookie.NewStore([]byte("hallen"))
	router.Use(sessions.Sessions("gin_session",store))

	router.Static("/static", "static")

	router.GET("/",Login)
	router.POST("/ti",Ti)

	router.Use(Middleware)
	router.GET("/list",Test)
	router.POST("/exit",Exit)
	router.POST("/start",Start)

	router.LoadHTMLGlob("temlpters/**/*")
	router.Run()
}
